/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistance;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import shared.Cpu;

/**
 *
 * @author chatelai
 */
public class DaoCpu {

    private static DatabaseConnector connector =null; ;
    private static DaoCpu instance;

    public static DaoCpu getInstance() throws SQLException {
        
        if (null == instance) { // Premier appel
            instance = new DaoCpu();
        }
        return instance;
    }

   public  void clear () throws SQLException {
       
       
             ResultSet resultat = connector.execQuery("select id from histo where type='cpu'");
           
//                        connector.execQuery("truncate table cpu");
//                        connector.execQuery("truncate table mem");
//                        connector.execQuery("truncate table histo");
//                        connector.execQuery("truncate table  lnkhistoprocess");
//                        connector.execQuery("truncate table  process");
              if (resultat!=null)   {   
            while (resultat.next()){
                
                connector.execUpdate("delete from  cpu where  histo_id="+resultat.getInt("id"));
                
            }
            }
            connector.execUpdate("delete from histo where type='cpu'");
       
    }

    private DaoCpu() throws SQLException {
        connector=DatabaseConnector.getInstance();
    }

    public void saveCpu(String serv,Cpu cpu) throws SQLException {
        Integer lastinserted_id = 0;
       
        ResultSet resultat = connector.execUpdate("INSERT INTO histo (serv,type) VALUES (\"" + serv + "\",\"cpu\")");
       


            if (resultat.next()) {
               lastinserted_id = resultat.getInt(1);
               resultat.close();
            } else {
               throw new SQLException("insertion ou récuperation de l'id impossible");
            }

           
       
        System.out.println(lastinserted_id);
        try {
            
       
        resultat = connector.execUpdate("INSERT INTO cpu (histo_id,us,  sy,  ni,  ids,  wa,  hi,  si,  st,loa5,loa1,loa15) VALUES (" + lastinserted_id + "," + cpu.getUs() + "," + cpu.getSy() + "," + cpu.getNi() + "," + cpu.getId() + "," + cpu.getWa() + "," + cpu.getHi() + "," + cpu.getSi() + "," + cpu.getSt() + "," + cpu.getLoa5() + "," + cpu.getLoa5() + "," + cpu.getLoa15() + ")");
        } catch (SQLException e) {
            resultat.close();
            connector.execUpdate("delete  from histo where id="+lastinserted_id);
           throw e;
        }
            if (resultat.next()) {
                lastinserted_id = resultat.getInt(1);
                resultat.close();
                
            } else {
                connector.execUpdate("delete  from histo where id="+lastinserted_id);
                throw new SQLException("insertion ou récuperation de l'id impossible");
            }

            
        
        
//            ResultSet resultat2 = connector.execQuery("INSERT INTO cpu (us,  sy,  ni,  id,  wa,  hi,  si,  st) VALUES (" + us + "," + sy + ")");

    }
   
    
    public Cpu getCpuByRequestId(int id) throws SQLException {
       
        System.out.println("select * from cpu where histo_id="+id);
       ResultSet resultat = DatabaseConnector.getInstance().execQuery("select * from cpu where histo_id="+id);
      Cpu cpu=null;
      
       while (resultat.next()) {
             cpu=new Cpu(resultat.getDouble("us"), resultat.getDouble("sy"), resultat.getDouble("ni"), resultat.getDouble("ids"), resultat.getDouble("wa"), resultat.getDouble("hi"), resultat.getDouble("si"), resultat.getDouble("st"), resultat.getDouble("loa1"), resultat.getDouble("loa5"), resultat.getDouble("loa15"));
                
            }
        
        return cpu;
    }
    

   

   
}
